var savebtn = false;
var regnum = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
var confirm = false;
$(function () {
    var bankname = $("#bankname").val();
    var banknum = $("#banknum").val();
    var binding_num = $("#binding_num").val();
    if (!isempty(bankname) && !isempty(banknum) && !isempty(binding_num))
        $("#bankinfo").html(bankname + '(' + banknum + ')');
    else
        getBankInfo();
    getUserAccount();
    remove_advertising();
    $("#confirm_btn").click(function () {
        if(confirm)
            location.href = "/except/files/user/my_spare.jsp";
    });
});

/**
 * 获取银行卡信息
 */
function getBankInfo() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = '/except/login.jsp';
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/bankcontroller/getbandingbanklist.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message
            if (code == 20000) {
                $("#bankinfo").html();
                var banknum = data.data[0].banknum;
                $("#bankinfo").html(data.data[0].bankname + '(' + banknum.substr(banknum.length - 4, 4) + ')');
                $("#binding_num").val(data.data[0].binding_num);
            } else if (code == 20013) {
                $("#bankinfo").html('请选择银行卡');
            } else if (code == 10001 || code == 10002)
                location.href = '/except/login.jsp';
            else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        }
    });
}

/**
 * 获取用户账户余额
 */
function getUserAccount() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    } else {
        var jsonstr = "{token:" + token + "}";
        var url = "/except/usercontroller/getmyspare.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                "json": jsonstr,
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                if (code == 20000) {
                    $("#money").html(data.data.sparemoney);
                } else if (code == 10001 || code == 10002) {
                    location.href = "/except/login.jsp";
                } else {
                    $("#hintbg").show();
                    $("#hint_message").html(message);
                }
            }
        });
    }
}

/**
 * 全部提现
 */
function allwithdraw() {
    var money = $("#money").html();
    $("#withdrawmoney").val(money);
}

/**
 * 确定提现
 */
function withdraw_apply() {
    if (savebtn)
        return;
    else {
        savebtn = true;
        var token = $("#token").val();
        if (isempty(token)) {
            location.href = "/except/login.jsp";
            return;
        } else {
            var binding_num = $("#binding_num").val();
            var withdrawmoney = $("#withdrawmoney").val();
            var money = $("#money").html();
            if (isempty(binding_num)) {
                $("#hintbg").show();
                $("#hint_message").html("请选择银行卡");
                savebtn = false;
                return;
            }
            if (isempty(withdrawmoney)) {
                $("#hintbg").show();
                $("#hint_message").html("请输入提现金额");
                savebtn = false;
                return;
            } else if (!regnum.test(withdrawmoney)) {
                $("#hintbg").show();
                $("#hint_message").html("请输入正确的提现金额（最多两位小数）");
                savebtn = false;
                return;
            }
            if (money * 1 < withdrawmoney * 1) {
                $("#hintbg").show();
                $("#hint_message").html("提现金额不能大于余额");
                savebtn = false;
                return;
            }
            var jsonstr = "{token:" + token + "}";
            var url = "/except/bankcontroller/withdrawapply.action";
            $.ajax({
                url: url,
                type: 'post',
                dataType: "json",
                data: {
                    "json": jsonstr,
                    bindingnum: binding_num,
                    withdrawmoney: withdrawmoney
                },
                async: false,
                success: function (data) {
                    var code = data.code;
                    var message = data.message;
                    if (code == 20000) {
                        $("#hintbg").show();
                        $("#hint_message").html("提现申请成功");
                        confirm = true;
                    } else if (code == 10001 || code == 10002) {
                        location.href = "/except/login.jsp";
                    } else {
                        savebtn = false;
                        $("#hintbg").show();
                        $("#hint_message").html(message);
                    }
                }
            });
        }
    }
}